package com.carlinfo.hospital.dao.impl;

import com.carlinfo.hospital.dao.PersonDao;
import com.carlinfo.hospital.pojo.Person;
import com.carlinfo.hospital.util.DataSourceUtil;
import com.carlinfo.hospital.util.PageInfoUtil;
import com.carlinfo.hospital.util.ReflectUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.math.BigInteger;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Repository
public class PersonDaoimpl implements PersonDao {
    @Autowired
    private DataSourceUtil dataSourceUtil;
    DataSource dataSource = dataSourceUtil.getInstance();
    QueryRunner queryRunner = new QueryRunner(dataSource);
    String sql = "";

    @Override
    public int saveOneDao(Person person) {
        // id,name,sex,orderId,content,status,createTime,updateTime,publicTime
        sql = "insert into person value(null,?,?,?,?,?,?,?,?,?)";
        DataSource dataSource = dataSourceUtil.getInstance();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        try {
            Object[] zh = ReflectUtil.zh(person);
            int execute = queryRunner.update(sql, zh);
            BigInteger query = queryRunner.query("select @@identity", new ScalarHandler<>(1));
            String id = query.toString();
            System.out.println(execute);
            return Integer.valueOf(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Person> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        sql = "select * from person where 1=1 ";
        List<Object> list = new ArrayList<Object>();
        if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + "")) {
            sql += " and name like ? ";
            list.add("%" + condMap.get("keyword") + "%");
        }
        if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + "")) {
            sql += " and status like ? ";
            list.add("%" + condMap.get("status") + "%");
        }
        if (condMap.get("stDate") != null && !"".equalsIgnoreCase(condMap.get("edDate") + "")) {
            sql += " and (publicTime>? and publicTime<?)";
            list.add(condMap.get("stDate"));
            list.add(condMap);
        }
        sql+=" order by createTime desc limit 10";
        System.out.println("sql:" + sql);
        String[] s = new String[list.size()];
        String[] strings = list.toArray(s);
        try {
            List<Person> res = queryRunner.query(sql, new BeanListHandler<Person>(Person.class), strings);
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Person selectOneDao(Map<String, Object> condMap) {
        List<Object> list = new ArrayList<Object>();
        if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null) {
            sql = "select * from person where name= ? ";
            list.add(condMap.get("name"));
        }
        if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null) {
            sql = "select * from person where id= ? ";
            list.add(condMap.get("id"));
        }
        try {
            Person res = queryRunner.query(sql, new BeanHandler<Person>(Person.class), list.get(0));
            return res;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int updateOneDao(Person person) {
        //name,sex,orderId,content,status,createTime,updateTime,publicTime
        sql = "update person set name=?,sex=?,identity=?,orderId=?,content=?,status=?,createTime=? ,updateTime=?,publicTime=? where id =?";
        try {
            System.out.println("person:"+person);
            Object[] zh = ReflectUtil.zh(person);
            Object[] zh1=new Object[zh.length+1];

            for (int i = 0; i <zh.length ; i++)
            {
                zh1[i]=zh[i];
            }
            zh1[zh1.length-1] = person.getId();
            int execute = queryRunner.execute(sql, zh1);
            return execute;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteOneDao(Map<String, Object> condMap) {
        List<Object> list = new ArrayList<Object>();
        if(!"".equalsIgnoreCase(condMap.get("name")+"")&&condMap.get("name")!=null)
        {
            sql="delete * from person where name= ? ";
            list.add(condMap.get("name"));
        }
        if(!"".equalsIgnoreCase(condMap.get("id")+"")&&condMap.get("id")!=null)
        {
            sql="delete * from person where id= ? ";
            list.add(condMap.get("id"));
        }
        String[] strings = new String[list.size()];
        try
        {
            int execute = queryRunner.execute(sql, list.toArray(strings));
            return execute;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;
    }
}
